package com.abc.base.spark.config;

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

import java.util.Properties;


public class SparkConfig {
    public static SparkSession getSparkSession() {
        return SparkSession.builder()
                .appName("House Analysis")
                .master("local[*]")
                .getOrCreate();

    }

    public static Dataset<Row> getDf(SparkSession spark, String tableName) {

        String mysqlUrl = "jdbc:mysql://node1:3306/test";
        Properties mysqlProperties = new Properties();
        mysqlProperties.setProperty("user", "root");
        mysqlProperties.setProperty("password", "123456");
        mysqlProperties.setProperty("driver", "com.mysql.cj.jdbc.Driver");
        return spark.read().jdbc(mysqlUrl, tableName, mysqlProperties);
    }

    public static void stopSparkSession(SparkSession spark) {
        if (spark != null) {
            spark.stop();
        }
    }

}
